/* replication do-file for Giovanni Capoccia, Lawrence Saez, and Eline de Rooij. 2012. 'When State Responses Fail: Religion and Secessionsm in India 1952-2002.' 
The Journal of Politics, 74(4): 1010-1022. */
/* see online Appendix for further information for a detailed discussion of operationalizations and data sources for the variables, 
and of the models and supplementary analyses: http://journals.cambridge.org/jop */

/* TABLE 1 */

use "CapocciaSaez&deRooij2012JOP_main.dta"

/* convert data to cross-sectional format */
collapse (mean) nsdp ndp tv_sri tv_state (max) state_change tv_violent tv_actor_repress pr_reason pr_rule pr_sep (first) firstyear=calyear (last) tv_state_id size2 tv_organization tv_rel_outlook emergence dissolution year lastyear=calyear original_dem or_dem shift_down shift_reason tv_border tv_state_rel tv_distance distance tv_enborder tv_enbor, by(id acronym name)
/* tv_state_name dropped */
tab shift_down tv_rel_outlook, col 

clear

/* TABLE 2 AND FIGURES 1 AND 2 */

/* re-load and set data for survival analysis */
use "CapocciaSaez&deRooij2012JOP_main.dta"
sort id year
stset year, failure(shift_down) id(id)

/* Kaplan-Meier survival estimate */
sts graph, gw lost

/* Variable selection: Log-rank test for equality across stata and univariate Cox models */
sts test tv_rel_outlook, logrank
sts test original_dem, logrank
sts test size2, logrank
sts test tv_violent, logrank
sts test tv_organization, logrank
sts test state_change, logrank
sts test pr_sep, logrank
sts test tv_actor_repress, logrank
sts test tv_enbor, logrank
sts test tv_state, logrank
sts test tv_state_rel, logrank

xi: stcox tv_rel_outlook, r efron nohr
xi: stcox original_dem, r efron nohr
xi: stcox size2, r efron nohr
xi: stcox tv_violent, r efron nohr
xi: stcox tv_organization, r efron nohr
xi: stcox state_change, r efron nohr
xi: stcox pr_sep, r efron nohr
xi: stcox tv_actor_repress, r efron nohr
xi: stcox tv_enbor, r efron nohr
xi: stcox distance, r efron nohr
xi: stcox tv_state, r efron nohr
xi: stcox i.tv_state_rel, r efron nohr
xi: stcox tv_sri, r efron nohr
/* conclusion: include all variables except pr_sep and tv_sri */

/* test of the assumption that the hazard is proportional */
xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel, r efron nohr schoenfeld(sche*) scaledsch(scae*)
estat phtest, detail

estat phtest, log plot(tv_rel_outlook) yline(0) mlabel(acronym)
estat phtest, log plot(original_dem) yline(0) mlabel(acronym)
estat phtest, log plot(size2) yline(0) mlabel(acronym)
estat phtest, log plot(tv_violent) yline(0) mlabel(acronym)
estat phtest, log plot(tv_organization) yline(0) mlabel(acronym)
estat phtest, log plot(state_change) yline(0) mlabel(acronym)
estat phtest, log plot(tv_actor_repress) yline(0) mlabel(acronym)
estat phtest, log plot(tv_enbor) yline(0) mlabel(acronym)
estat phtest, log plot(tv_state) yline(0) mlabel(acronym)
estat phtest, log plot(distance) yline(0) mlabel(acronym)
estat phtest, log plot(_Itv_state__1) yline(0) mlabel(acronym)
estat phtest, log plot(_Itv_state__2) yline(0) mlabel(acronym)
estat phtest, log plot(_Itv_state__3) yline(0) mlabel(acronym)

xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel, r efron nohr tvc(original_dem) texp(ln(_t)) 
xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel, r efron nohr tvc(state_change) texp(ln(_t)) 
xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel, r efron nohr tvc(tv_enbor) texp(ln(_t)) 
xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel, r efron nohr tvc(_Itv_state__1 _Itv_state__2 _Itv_state__3) texp(ln(_t)) 
/* conclusion: include interaction with natural logarithm of survival time and original_dem, state_change, tv_enbor. 
Too few cases to interact with tv_state_rel 'other minority' dummy (_Itv_state__3) */

/* Table 2 */
xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel, r efron nohr tvc(original_dem state_change tv_enbor) texp(ln(_t)) 
estat ic
stcox

/* Figures 1 and 2 */
/* manually create interaction with the natural logarithm of survival time */ 
gen or_demtvc = original_dem*(ln(_t)) 
gen st_chtvc = state_change*(ln(_t))
gen enbortvc= tv_enbor*(ln(_t))
gen rel_outtvc= tv_rel_outlook*(ln(_t))

xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron nohr basech(inthaz) basehc(haz) basesurv(surv0)
twoway line surv0 _t, sort connect(J) title("Baseline S(t)") legend(order(1 "Baseline")) 
twoway line inthaz _t, sort connect(J) 

gen rel = tv_rel_outlook-1
gen dem = original_dem-1
gen siz = size2-1
gen vio= tv_violent-1
gen org = tv_organization-1
gen cha =  state_change-1
gen rep = tv_actor_repress-1
gen bor= tv_enbor-1
gen sta= tv_state-1

quietly xi: stcox rel original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv1) 
twoway (line surv0 _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(solid)) (line surv1 _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(dash)), yscale(range(0 1)) ylabel(0(.1)1) title("Baseline S(t), religion") legend(order(1 "Nonreligious actors" 2 "Religious actors"))

quietly xi: stcox tv_rel_outlook original_dem size2 tv_violent tv_organization state_change rep tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv2a)
quietly xi: stcox rel original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv2b)
quietly xi: stcox rel original_dem size2 tv_violent tv_organization state_change rep tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv2c)
twoway (line surv0 _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(solid)) (line surv2a _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(shortdash)) (line surv2b _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(longdash_dot_dot)) (line surv2c _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(longdash)), yscale(range(0 1)) ylabel(0(.1)1) title("Baseline S(t), religion and repression") legend(order(1 "Nonreligious, no repression" 2 "Nonreligious, repression" 3 "Religious, no repression" 4 "Religious, repression"))

quietly xi: stcox tv_rel_outlook original_dem size2 tv_violent org state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv5a)
quietly xi: stcox rel original_dem size2 tv_violent tv_organization state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv5b)
quietly xi: stcox rel original_dem size2 tv_violent org state_change tv_actor_repress tv_enbor distance tv_state i.tv_state_rel or_demtvc st_chtvc enbortvc, r efron basesurv(surv5c)
twoway (line surv0 _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(solid)) (line surv5a _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(shortdash)) (line surv5b _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(longdash_dot_dot)) (line surv5c _t if _t>=0 & _t<=50, sort lcolor(black) lwidth(medthick) lpattern(longdash)), yscale(range(0 1)) ylabel(0(.1)1) title("Baseline S(t), religion and inclusion in the electoral process") legend(order(1 "Nonrel. nonparty groups" 2 "Nonrel. parties" 3 "Rel. nonparty groups" 4 "Rel. parties"))

clear

/* TABLE 3 */

/* load and set data */

use "CapocciaSaez&deRooij2012JOP_repression.dta"

label variable all_repression "Number of succesive years with either pr or repression of at least 1 actor"
label variable all_sep_repression "Number of succesive years with either pr against separatist movements or repression of at least 1 actor"
label variable rel_repression "Number of succesive years with either pr or repression of at least 1 religious actor"
label variable rel_sep_repression "Number of succesive years with either pr against separatist movements or repression of at least 1 religious actor"

tsset state_id calyear, yearly

/* Table 3 */
xtabond total_religious l.rel_sep_repression, r
estat abond

/* Wald test that all coef except constant are zero */
test L1.total_religious L1.rel_sep_repression

/* use Sagan from non-robust model */
quietly: xtabond total_religious l.rel_sep_repression
estat sargan

quietly: xtabond total_religious l.rel_sep_repression, lag(40)
estat sargan 
/* conclusion: reject the null hypothesis that the over-identifying restrictions of the model are valid. 
However, the test has a tendency to over-reject in the presence of heteroskedasticity. 
Including all possible lags of the dependent variable avoids rejecting the null hypothesis that the over-identifying restrictions are valid
but does not change the results of the model substantively.*/

